define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form){

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'financing/supplementary/index',
                    //add_url: 'users/user/add',
                    edit_url: 'financing/supplementary/edit',
                    //del_url: 'users/user/del',
                    //for_url: 'financing/accepts/for',
                    multi_url: 'financing/supplementary/multi',
                    table: 'supplementary',
                }
            });

            var table = $("#table");

            //console.log($('#table').bootstrapTable('getAllSelections'));
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                //sortName: 'user.id',
                commonSearch: true,
                searchFormVisible: true,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                return ++index;
                            }, operate: false},
                        {field: 'code', title: __('服务单号'), operate: 'LIKE'},
                        {field: 'info.name', title: __('就诊人'), operate: 'LIKE'},
                        {field: 'info.sex', title: __('性别'), operate: false},
                        {field: 'age', title: __('年龄'), operate: false},
                        {field: 'receivable', title: __('应收金额（元）'), operate: false},
                        {field: 'on_account', title: __('赊账金额（元）'), operate: false},
                        //{field: 'pays', title: __('支付方式'), operate: false},
                        //{field: 'payees', title: __('收款人'), operate: false},
                        //{field: 'image', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        //{field: 'draw', title: __('抽奖次数'), operate: false, sortable: true},
                        {field: 'on_credit', title: __("状态"), searchList: {"1":__('未补缴'),"2":__('已补交'),"3":__('已关闭')}, formatter: Table.api.formatter.status},
                        //{field: 'userss.name', title: __('上级姓名'), operate: 'LIKE', sortable: true},
                        {field: 'payment_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value, row, index){
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                // 所有按钮名称
                                var names = [];
                                buttons.forEach(function (item) {
                                    names.push(item.name);
                                });
                                if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
                                    buttons.push(Table.button.dragsort);
                                }
                                if (options.extend.edit_url !== '' && names.indexOf('edit') === -1 && row.on_credit == 1) {
                                    Table.button.edit.url = options.extend.edit_url;
                                    buttons.push(Table.button.edit);
                                }
                                // console.log(row);
                                //删除按钮 添加 条件 row.pid !== 0
                                if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
                                    buttons.push(Table.button.del);
                                }
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            },
                            buttons: [{
                                name: '日志',
                                text: __('日志'),
                                //icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'financing/debt/index?order_id={id}'
                            }],
                        }

                        // {field: 'operate', title: __('Operate'), table: table,
                        //     events: Table.api.events.operate,
                        //     buttons: [{
                        //         name: 'detail',
                        //         //extend: 'data-area=\'["100%", "100%"]\'',
                        //         text: __('补交'),
                        //         icon: 'fa fa-list',
                        //         classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                        //         url: 'financing/supplementary/fors'
                        //     }],
                        //     formatter: Table.api.formatter.operate
                        //
                        // },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});